<template>
  <div class="content">
    <div class="header">
      <h3 class="title">{{ contentConfig?.header?.title }}</h3>
      <el-button type="primary" @click="handleNewUserClick" v-if="isCreate">{{
        contentConfig?.header?.btnTitle ?? "新建数据"
      }}</el-button>
    </div>
    <div class="table">
      <el-table
        :data="pageList"
        style="width: 100%"
        border
        v-bind="contentConfig.childrenTree"
      >
        <template v-for="item in contentConfig.propList" :key="item.prop">
          <!-- 创建时间/更新时间 -->
          <template v-if="item.type === 'timer'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                {{ scope.row[item.prop] }}
              </template>
            </el-table-column>
          </template>
          <!-- 操作按钮 -->
          <template v-else-if="item.type === 'handler'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                <el-button
                  v-if="isUpdate"
                  link
                  type="primary"
                  size="small"
                  text
                  icon="Edit"
                  @click="handleEditBtnClick(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  v-if="isDelete"
                  link
                  type="danger"
                  size="small"
                  text
                  icon="Delete"
                  @click="handleDeleteClick(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </template>

          <!-- 定制 -->
          <template v-else-if="item.type === 'custom'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                <!-- 自定义内容使用具名插槽 -->
                <slot :name="item.slotName" v-bind="scope" :prop="item.prop">
                </slot>
              </template>
            </el-table-column>
          </template>

          <!-- 普通 -->
          <template v-else>
            <el-table-column align="center" v-bind="item" />
          </template>
        </template>
        <!-- 操作 -->
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, defineEmits } from "vue"
// import { formatUTC } from "@/utils/format"
import { storeToRefs } from "pinia"
import useSystemStore from "@/stores/main/system/system"
import usePermissions from "@/hooks/usePermissions"

// 定义接口
interface IProps {
  contentConfig: {
    pageName: string
    header?: {
      title?: string
      btnTitle: string
    }
    propList: any[]
    childrenTree?: any
  }
}
const props = defineProps<IProps>()

// 0获取是否有对对应的增删改查的权限
// const loginStore = useLoginStroe()
// const { permissions } = loginStore
const isCreate = usePermissions(`${props.contentConfig.pageName}:create`)
const isDelete = usePermissions(`${props.contentConfig.pageName}:delete`)
const isUpdate = usePermissions(`${props.contentConfig.pageName}:update`)
const isQuery = usePermissions(`${props.contentConfig.pageName}:query`)
// const isCreate = permissions.find((item) =>
//   item.includes(`${props.contentConfig.pageName}:create`)
// )
// const isDelete = permissions.find((item) =>
//   item.includes(`${props.contentConfig.pageName}:delete`)
// )
// const isUpdate = permissions.find((item) =>
//   item.includes(`${props.contentConfig.pageName}:update`)
// )
// const isQuery = permissions.find((item) =>
//   item.includes(`${props.contentConfig.pageName}:query`)
// )

// 自定义事件
const emit = defineEmits(["newClick", "editClick"])
const systemStore = useSystemStore()
// 页码相关的逻辑
const currentPage = ref(1)
const pageSize = ref(10)
//
systemStore.$onAction(({ name, after }) => {
  after(() => {
    if (
      name === "deletePageByIdAction" ||
      name === "editPageDataDataAction" ||
      name === "newPageDataAction"
    ) {
      currentPage.value = 1
    }
  })
})

// systemStore.postUserListAction()
fetchPageListDate()
// 2.获取usersList数据,进行展示
const { pageList, pageTotalCount } = storeToRefs(systemStore)
function handleSizeChange() {
  fetchPageListDate()
}
function handleCurrentChange() {
  fetchPageListDate()
}
//定义函数发送网络请求
function fetchPageListDate(formData: any = {}) {
  //如果没有查询权限直接返回
  if (!isQuery) return
  //1获取size,offset
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  const queryInfo = { ...pageInfo, ...formData }
  systemStore.postPageListAction(props.contentConfig.pageName, queryInfo)
}
// 删除数据
function handleDeleteClick(id: number) {
  systemStore.deletePageByIdAction(props.contentConfig.pageName, id)
  console.log(id)
}
// 编辑用户
function handleEditBtnClick(data: any) {
  emit("editClick", data)
  // console.log(data)
}
// 新建用户
function handleNewUserClick() {
  emit("newClick")
}

//

defineExpose({ fetchPageListDate })
</script>
<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
